package org.example.server.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.common.result.Result;
import org.example.pojo.dto.ChatMessageDTO;
import org.example.server.sever.ChatService;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/ai/chat")
@RequiredArgsConstructor
public class ChatController {

    private final ChatService chatService;

    /**
     * 发送消息并与AI进行对话
     * @param chatMessageDto 包含用户消息和用户ID的对象
     * @return AI回复的内容
     */
    @PostMapping("/message")
    public Result<String> chat(@RequestBody ChatMessageDTO chatMessageDto) {
        try {
            // 使用用户ID作为记忆ID，确保不同用户有不同的聊天记录
            String response = chatService.chat(chatMessageDto.getBoxId(), chatMessageDto.getMessage());
            return Result.success(response);
        } catch (Exception e) {
            log.error("AI聊天出错: ", e);
            return Result.error("AI聊天出错: " + e.getMessage());
        }
    }

    /**
     * 清除指定用户的聊天记录
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/memory/{userId}")
    public Result<String> clearMemory(@PathVariable String userId) {
        try {
            // 清除指定用户的聊天记忆
            chatService.clearMemory(userId);
            return Result.success("聊天记录已清除");
        } catch (Exception e) {
            log.error("清除聊天记录出错: ", e);
            return Result.error("清除聊天记录出错: " + e.getMessage());
        }
    }
    @PostMapping("/messa")
    public Result<String> chat(String message) {
        return  Result.success(chatService.chat("123", message));
    }
}